//
//  EvaluateServiceViewController.m
//  onlineCustoSDK
//
//  Created by 曹宗华 on 2018/8/2.
//  Copyright © 2018年 com.netinfosz. All rights reserved.
//

#import "EvaluateServiceViewController.h"

@interface EvaluateServiceViewController ()<UITextViewDelegate,starViewDelegate>
{
    int level;
    evaluateOption option;
}
@property(nonatomic,strong)UILabel *label;
@property(nonatomic,strong)UITextView *messageTextView;
@property(nonatomic,strong)UIButton *commitBtn;
@property(nonatomic,strong)CJTStarView *starViw;
@property(nonatomic,strong)UILabel *evalutaLabel;
@property(nonatomic,strong)NSString *evaluateContent;
@end

@implementation EvaluateServiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"服务评价";
    self.label.hidden=NO;
    self.messageTextView.hidden=NO;
    self.commitBtn.hidden=NO;
    self.starViw.hidden=NO;
    self.evalutaLabel.hidden=NO;
  
    // Do any additional setup after loading the view.
}
- (UILabel*)label
{
    if (!_label) {
        _label=[[UILabel alloc] initWithFrame:CGRectMake((App_Frame_Width-App_Frame_Width*0.6)*0.5, kNaviBarHeight+20, App_Frame_Width*0.6, 30)];
        _label.textAlignment=NSTextAlignmentCenter;
        _label.text=@"请对我的服务做出评价";
        [self.view addSubview:_label];
    }
    return _label;
}
- (CJTStarView*)starViw
{
    if (!_starViw) {
        _starViw=[[CJTStarView alloc] initWithFrame:CGRectMake(100, self.label.y+self.label.height+20, App_Frame_Width-200,50 ) starCount:5];
        _starViw.openHalf=NO;
        _starViw.delegate=self;
        [self.view addSubview:_starViw];
    }
    return _starViw;
}
- (UILabel*)evalutaLabel
{
    if (!_evalutaLabel)
    {
        _evalutaLabel=[[UILabel alloc] initWithFrame:CGRectMake((App_Frame_Width-150)/2, self.starViw.y+self.starViw.height+10, 150, 30)];
        _evalutaLabel.textAlignment=NSTextAlignmentCenter;
        _evalutaLabel.textColor=[UIColor redColor];
        _evalutaLabel.font=[UIFont systemFontOfSize:18];
        [self.view addSubview:_evalutaLabel];
    }
    return _evalutaLabel;
}
- (UITextView*)messageTextView
{
    if(!_messageTextView)
    {
        _messageTextView=[[UITextView alloc] initWithFrame:CGRectMake(20, self.evalutaLabel.y+self.evalutaLabel.height+20, App_Frame_Width-40, 60)];
        _messageTextView.delegate=self;
        _messageTextView.returnKeyType=UIReturnKeyDone;
        _messageTextView.backgroundColor=[UIColor lightGrayColor];
        [self.view addSubview:_messageTextView];
    }
    return _messageTextView;
}
- (UIButton*)commitBtn
{
    if(!_commitBtn)
    {
        _commitBtn=[[UIButton alloc] init];
        _commitBtn.frame=CGRectMake(20, self.messageTextView.y+self.messageTextView.height+20, self.messageTextView.width, 38);
        _commitBtn.backgroundColor=[UIColor  blueColor];
        [_commitBtn setTitle:@"提交" forState:UIControlStateNormal];
        [_commitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_commitBtn addTarget:self action:@selector(commitEvaluateContent) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_commitBtn];
    }
    return _commitBtn;
}
- (void)commitEvaluateContent
{
    NSLog(@"提交了评论");
    if(self.transferId==nil)
    {
        [CCDemoUtil showAlertWithTitle:@"提示" content:@"转接流水号不能为空"];
        return;
    }
    if(self.evaluateContent==nil)
    {
        [CCDemoUtil showAlertWithTitle:@"提示" content:@"评价内容不能为空"];
    }
    [[YiChatClient shareInstance] evalutionTransferId:self.transferId Level:option Content:self.evaluateContent Success:^(NSString *info) {
       
        NSData *data=[info dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *receiveDic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
         NSLog(@"评价成功结果:%@",receiveDic);
        if ([receiveDic[@"resultCode"] intValue]==0) {
            [CCDemoUtil showAlertWithTitle:@"提示" content:@"评价成功"];
        }
        
    } fail:^(NSError *error) {
        NSLog(@"评价失败结果:%@",error.localizedDescription);
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)coutStar:(id)starview
{
    CJTStarView *view=(CJTStarView*)starview;
      self.messageTextView.text=[NSString stringWithFormat:@"%f",view.nowScore];
    level=ceil(view.nowScore);// 转换成比小数大的整数
    switch (level) {
        case 1:
        {
            option= evaluateGreateNoSatisfy;
            self.evalutaLabel.text=@"非常不满意";
        }
            break;
        case 2:
        {
            option=evaluateNoSatisfy;
             self.evalutaLabel.text=@"不满意";
        }
            break;
        case 3:
        {
            option=evaluateOrdinary;
             self.evalutaLabel.text=@"一般";
        }
            break;
        case 4:
        {
            option=evaluateSatisfy;
            self.evalutaLabel.text=@"满意";
        }
            break;
        case 5:
        {
            option=evaluateGreateSatisfy;
             self.evalutaLabel.text=@"非常满意";
        }
            break;
            
        default:
            break;
    }
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    NSLog(@"touchesBegan:withEvent:");
    [self.view endEditing:YES];
//    [super touchesBegan:touches withEvent:event];
    self.evaluateContent=self.messageTextView.text;
}
//文本视图委托代理
#pragma mark - UITextViewDelegate
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    NSLog(@"即将开始输入...");
    return YES;//返回YES允许输入，NO禁止输入
}

- (BOOL)textViewShouldEndEditing:(UITextView *)textView
{
    NSLog(@"即将结束输入...");
    return YES;
}

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    NSLog(@"已经开始输入了...");
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    NSLog(@"已经结束输入了...");
    self.evaluateContent=textView.text;
   NSLog(@"评论endEditing:%@",self.evaluateContent);
}

- (void)textViewDidChange:(UITextView *)textView
{
    NSString *str = textView.text;//文本内容变化时调用
    NSLog(@"%@", str);
}

//点击return键或delete键时调用
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    //如果点击了return键，则收起键盘
    if ([text isEqualToString:@"\n"])
    {
        NSLog(@"return键点击了");
       self.evaluateContent=textView.text;
        NSLog(@"评论:%@",self.evaluateContent);
        [textView resignFirstResponder];//辞去第一响应
    }
    
    //如果点击了delete键
    if ([text isEqualToString:@""])
    {
         self.evaluateContent=textView.text;
//        NSLog(@"delete键点击了");
    }
    return YES;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
